/*
 * Copyright 2023 AntGroup CO., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

package com.antgroup.geaflow.dsl.udf.table.date;

import com.antgroup.geaflow.common.exception.GeaflowRuntimeException;
import com.antgroup.geaflow.dsl.common.function.Description;
import com.antgroup.geaflow.dsl.common.function.UDF;
import java.util.Calendar;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

@Description(name = "date_trunc", description = "Returns date truncated to the unit specified by "
    + "the format.")
public class DateTrunc extends UDF {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormat.forPattern(
        "yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd");
    private static final String DATE_FORMAT = "%04d-%02d-%02d %02d:%02d:%02d";
    private final Calendar calendar = Calendar.getInstance();

    public String eval(String date, String datepart) {

        if (date == null || datepart == null) {
            return null;
        }

        DateTimeFormatter formatter;

        if (date != null && date.length() <= 10) {
            formatter = DATE_FORMATTER;
        } else {
            formatter = DATE_TIME_FORMATTER;
        }

        try {
            calendar.setTime(formatter.parseDateTime(date).toDate());
        } catch (Exception e) {
            throw new GeaflowRuntimeException(e);
        }

        switch (datepart.toLowerCase()) {
            case "yyyy" :
            case "year" :
                return String.format(DATE_FORMAT, calendar.get(Calendar.YEAR), 1, 1, 0, 0, 0);
            case "mm" :
            case "mon" :
            case "month" :
                return String.format(DATE_FORMAT, calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
            case "dd" :
            case "day" :
                return String.format(DATE_FORMAT, calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            case "hh" :
            case "hour" :
                return String.format(DATE_FORMAT, calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH),
                    calendar.get(Calendar.HOUR_OF_DAY), 0, 0);

            case "mi" :
            case "minute" :
                return String.format(DATE_FORMAT, calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH),
                    calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), 0);

            case "ss" :
            case "second" :
                return String.format(DATE_FORMAT, calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH),
                    calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE),
                    calendar.get(Calendar.SECOND));

            default:
                throw new RuntimeException("unknown datepart:" + datepart);
        }
    }

}
